const port = process.env.port || process.env.npm_config_port || 9529// dev port
const path = require('path');

function resolve(dir) {
  return path.join(__dirname, dir)
}




module.exports = {
  publicPath: './',
  lintOnSave: false,
  productionSourceMap: false, // 清除 .map 文件
  css: {
    loaderOptions: {
      less: { javascriptEnabled: true },
       postcss: {
                plugins: [
                    require('postcss-px2rem')({
                        // 以设计稿750为例， 750 / 10 = 75
                        remUnit: 192
                    }),
                ]
            }
    }
  },
  runtimeCompiler:true,
  pwa: {

    iconPaths: {

      favicon32: 'favicon.ico',

      favicon16: 'favicon.ico',

      appleTouchIcon: 'favicon.ico',

      maskIcon: 'favicon.ico',

      msTileImage: 'favicon.ico'

    }

  },
  chainWebpack(config) {
    // set svg-sprite-loader
    config.resolve.alias
      .set("@", resolve("src"))
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()



    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              maxInitialRequests: 20,
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial'
                },

                icons: {
                  name: 'chunk-icons',
                  priority: 24,
                  test: /[\\/]node_modules[\\/]_?@ant-design(.*)/
                },

                moment: {
                  name: 'chunk-moment',
                  priority: 18,
                  test: /[\\/]node_modules[\\/]_?moment(.*)/
                },

                es: {
                  name: 'chunk-es',
                  priority: 23,
                  test: /[\\/]node_modules[\\/]_?ant-design-vue[\\/]es/
                },
                lib: {
                  name: 'chunk-lib',
                  priority: 19,
                  test: /[\\/]node_modules[\\/]_?echarts[\\/]lib/
                },
                echart: {
                  name: 'chunk-echart',
                  priority: 18,
                  test: /[\\/]node_modules[\\/]_?echarts[\\/]lib[\\/]chart/
                },
                main: {
                  name: 'chunk-main',
                  priority: 28,
                  test: resolve('src/main.js')
                },

                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'),
                  minChunks: 3,
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        })
  },
  configureWebpack: {
    resolve: {
      extensions: ['js', 'vue'],
      alias: {
        '@/': resolve('src')
      }
    }
  },

  devServer: {
    port: port,
  }
}
